package com.kim.rssreader.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlTagFilter {

	private static final String patternStr = "<[.[^<]]*>";
	private static final Pattern pattern = Pattern.compile(patternStr,
			Pattern.CASE_INSENSITIVE);

	private static final String patterStr_tag1 = "&#[0-9]*;";
	// private static final String patterStr_tag2 = "&#8221;";
	// private static final String patterStr_tag3 = "&#8230;";
	// private static final String patterStr_tag4 = "&#8212;";

	private static final String patterStr_tag_a_start = "<a .+?>";
	private static final String patterStr_tag_a_end = "</a>";

	private static final String patterStr_tag_font = "font-size:[0-9][px]|[pt];";

	private static final String patterStr_tag_text_indent = "text-indent:[\\d]em;";

	private static final String patterStr_tag_p = "<p>　　";

	private static final String patterStr_tag_img = "<img";

	private static final Pattern pattern_tag1 = Pattern.compile(patterStr_tag1,
			Pattern.CASE_INSENSITIVE);
	// private static final Pattern pattern_tag2 =
	// Pattern.compile(patterStr_tag2,
	// Pattern.CASE_INSENSITIVE);
	// private static final Pattern pattern_tag3 =
	// Pattern.compile(patterStr_tag3,
	// Pattern.CASE_INSENSITIVE);
	// private static final Pattern pattern_tag4 =
	// Pattern.compile(patterStr_tag4,
	// Pattern.CASE_INSENSITIVE);
	private static final Pattern pattern_a_start = Pattern.compile(
			patterStr_tag_a_start, Pattern.CASE_INSENSITIVE);
	private static final Pattern pattern_a_end = Pattern.compile(
			patterStr_tag_a_end, Pattern.CASE_INSENSITIVE);

	private static final Pattern pattern_font = Pattern
			.compile(patterStr_tag_font);

	private static final Pattern patter_txt_indent = Pattern
			.compile(patterStr_tag_text_indent);

	private static final Pattern patter_p = Pattern.compile(patterStr_tag_p);

	private static final Pattern patter_img = Pattern
			.compile(patterStr_tag_img);

	static Matcher matcher = null;

	public static String filter(String content, boolean isTitle) {
		if (null == content || "".equals(content)) {
			return "";
		}
		matcher = pattern.matcher(content);
		content = matcher.replaceAll("");

		matcher = pattern_tag1.matcher(content);
		content = matcher.replaceAll("");

		// matcher = pattern_tag2.matcher(content);
		// content = matcher.replaceAll("");
		//
		// matcher = pattern_tag3.matcher(content);
		// content = matcher.replaceAll("");
		//
		// matcher = pattern_tag4.matcher(content);
		// content = matcher.replaceAll("");

		content = content.replaceAll("　+", "");
		content = content.replaceAll("\n", " ");
		content = content.trim();
		if (!isTitle) {
			content = "\t\t" + content;
		}
		return content;

	}

	// private static final String patterStr_js_start = "<javascript";
	// private static final String patterStr_js_end = "</javascript>";
	// private static final String patterStr_js_end1 = "/>";

	/**
	 * 过滤rss内容
	 * 
	 * @param content
	 * @return
	 */
	public static String filterContent(String content) {

		// 过滤js
		// int jsStartIndex = content.indexOf(patterStr_js_start);
		//
		// if (jsStartIndex == -1) {
		// return content;
		// }
		//
		// int jsEndIndex1 = content.indexOf(patterStr_js_end, jsStartIndex);
		// int jsEndIndex2 = content.indexOf(patterStr_js_end1, jsStartIndex);
		//
		// if (jsEndIndex1 == -1 && jsEndIndex2 == -1) {
		// return content;
		// }

		return content;
	}

	public static String filterFlagA(String content) {
		String temp = content;

		if (temp != null) {

			Matcher matcher = pattern_a_start.matcher(temp);
			temp = matcher.replaceAll("");

			matcher = pattern_a_end.matcher(temp);
			temp = matcher.replaceAll("");

		}

		return temp;
	}

	public static String filterFlagFont(String content) {
		String temp = content;

		if (temp != null) {

			Matcher matcher = pattern_font.matcher(temp);
			temp = matcher.replaceAll("");
			matcher = null;
		}

		return temp;
	}

	public static String filterFlagIndent(String content) {
		String temp = content;

		if (temp != null) {

			Matcher matcher = patter_txt_indent.matcher(temp);
			temp = matcher.replaceAll("");
			matcher = null;
		}

		return temp;
	}

	public static String filterFlagPSpace(String content) {
		String temp = content;

		if (temp != null) {

			Matcher matcher = patter_p.matcher(temp);
			temp = matcher.replaceAll("");
			matcher = null;
		}

		return temp;
	}

	public static String filterFlagImg(String content) {
		String temp = content;

		if (temp != null) {

			Matcher matcher = patter_img.matcher(temp);
			temp = matcher.replaceAll("<p  style=\"text-indent:0em;\"><img ");
			/*
			 * temp = matcher .replaceAll(
			 * "<p  style=\"text-indent:0em;\"><img onClick=\"javascript:alert(this.src);\" "
			 * );
			 */
			temp = matcher
					.replaceAll("<p align=\"center;\"  style=\"text-indent:0em;\"><img onClick=\"javascript:alert(this.src);\" ");
			matcher = null;
		}

		return temp;
	}

	/**
	 * 过滤无效符号
	 * 
	 * @param content
	 * @return
	 */
	public static String filterFlagInvalidSymbol(String content) {
		String temp = content;

		if (temp != null) {
			temp = temp.replaceAll("&nbs", "");
		}

		return temp;
	}

}
